package com.heima.wemedia.controller.v1;


import cn.hutool.core.util.ObjectUtil;
import com.heima.common.dtos.ResponseResult;
import com.heima.wemedia.dto.WmNewsDto;
import com.heima.wemedia.dto.WmNewsPageReqDto;
import com.heima.wemedia.pojo.WmNews;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController {

    @Resource
    private WmNewsService wmNewsService;

    @PostMapping("/list")
    public ResponseResult<List<WmNews>> findAll(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findAll(dto);
    }

    @PostMapping("/submit")
    public ResponseResult<Object> submitNews(@RequestBody WmNewsDto dto) {
        return this.wmNewsService.submitNews(dto);
    }

    @GetMapping("one/{id}")
    public ResponseResult<WmNews> checkDetail(@PathVariable("id") Integer id) {
        WmNews wmNews = this.wmNewsService.checkDetail(id);
        if (ObjectUtil.isEmpty(wmNews)) {
            ResponseResult.errorResult(1002, "文章不存在");
        }
        return ResponseResult.okResult(wmNews);
    }

    @GetMapping("del_news/{id}")
    public ResponseResult deleteNew(@PathVariable("id") Integer id) {
        return this.wmNewsService.deleteNew(id);
    }

    @PostMapping("down_or_up")
    public ResponseResult updateStatus(@RequestBody WmNewsDto dto) {
        return this.wmNewsService.updateStatus(dto);
    }

}
